package com.ellricky.baselib.ui.cla

import android.view.View
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import kotlinx.android.synthetic.main.fragment_official_class.*

class OfficialClassFragment : BaseFragment<Void, BasePresenter<Void>>() {

    private var ePickerUtil: EPickerUtil? = null
    private var selectCourseType: String = ""
    private var selectCourseState: String = ""
    private var selectCourseWare: String = ""
    private var selectCourseKind: String = ""
    private var selectCourseNumber: String = ""
    private var selectClassRoom: String = ""
    private var selectClassStartDate: String = ""
    private var selectClassStartTime: String = ""
    private var selectClassEndTime: String = ""
    private var className: String = ""
    private var classType: String = BaseConstant.ITEM_CLASS_A.toString()

    private val courseKindList: ArrayList<IdNamePickerEntity> by lazy {
        arrayListOf(
            IdNamePickerEntity("11", "基础课时"),
            IdNamePickerEntity("12", "特殊课时"),
            IdNamePickerEntity("33", "艺术课时"),
            IdNamePickerEntity("62", "赠送课时"),
        )
    }

    private val courseCourseNumberList: ArrayList<IdNamePickerEntity> by lazy {
        arrayListOf(
            IdNamePickerEntity("1", "1"),
            IdNamePickerEntity("2", "2"),
            IdNamePickerEntity("3", "3"),
            IdNamePickerEntity("4", "4"),
            IdNamePickerEntity("5", "5"),
            IdNamePickerEntity("6", "6"),
            IdNamePickerEntity("7", "7"),
            IdNamePickerEntity("8", "8"),
            IdNamePickerEntity("9", "9"),
            IdNamePickerEntity("10", "10"),
            IdNamePickerEntity("11", "11"),
            IdNamePickerEntity("12", "12"),
            IdNamePickerEntity("13", "13"),
            IdNamePickerEntity("14", "14"),
            IdNamePickerEntity("15", "15"),
            IdNamePickerEntity("16", "16"),
            IdNamePickerEntity("17", "17"),
            IdNamePickerEntity("18", "18"),
            IdNamePickerEntity("19", "19"),
            IdNamePickerEntity("20", "20"),
        )
    }

    companion object {
        fun newInstance(): OfficialClassFragment {
            return OfficialClassFragment()
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.fragment_official_class
    }

    override fun initUIView(p0: View?) {
        ePickerUtil = EPickerUtil(activity)

        label.setLabels(arrayListOf("2V6", "1V6"))
        label.setOnLabelSelectChangeListener { label,
                                               data,
                                               isSelect,
                                               position ->
            if (isSelect) {
                val dicName = data as String
                classType = if (dicName == "1V6") {
                    BaseConstant.ITEM_CLASS_B.toString()
                } else {
                    BaseConstant.ITEM_CLASS_A.toString()
                }
            }
        }

        etClassType.setOnClickListener(this)
        etClassStage.setOnClickListener(this)
        etClass.setOnClickListener(this)
        etClassKind.setOnClickListener(this)
        etClassNumber.setOnClickListener(this)
        etClassRoom.setOnClickListener(this)
        etClassStartDate.setOnClickListener(this)
        etClassStartTime.setOnClickListener(this)
        etClassEndTime.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            //课程类型
            R.id.etClassType -> {
                ePickerUtil?.checkClassTypePicker(null) { _, t ->
                    selectCourseType = t.id
                    etClassType.value = t.name
                }
            }
            //课程阶段
            R.id.etClassStage -> {
                if (selectCourseType.isEmpty()) {
                    ToastUtil.show(context, "请先选择课程类型")
                    return
                }
                ePickerUtil?.checkClassStatePicker(selectCourseType, null) { _, t ->
                    selectCourseState = t.id
                    etClassStage.value = t.name
                }
            }
            //课件
            R.id.etClass -> {
                if (selectCourseType.isEmpty()) {
                    ToastUtil.show(context, "请先选择课程类型")
                    return
                }
                ePickerUtil?.checkClassDatePicker(null) { _, t ->
                    selectCourseWare = t.id
                    etClass.value = t.name
                }
            }
            //扣课种类
            R.id.etClassKind -> {
                ePickerUtil?.showClassKindPicker(courseKindList, null) { _, t ->
                    selectCourseKind = t.id
                    etClassKind.value = t.name
                }
            }
            //扣课节数
            R.id.etClassNumber -> {
                ePickerUtil?.showClassNumberPicker(courseCourseNumberList, null) { _, t ->
                    selectCourseNumber = t.id
                    etClassNumber.value = t.name
                }
            }
            //教室列表
            R.id.etClassRoom -> {
                ePickerUtil?.checkClassRoomPicker(null) { _, t ->
                    selectClassRoom = t.id
                    etClassRoom.value = t.name
                }
            }
            //开课日期
            R.id.etClassStartDate -> {
                ePickerUtil?.showStartDatePicker("") { content,
                                                       _,
                                                       _ ->
                    selectClassStartDate = content
                    etClassStartDate.value = content
                }
            }
            //上课时间
            R.id.etClassStartTime -> {
                ePickerUtil?.showStartTimePicker("") { content,
                                                       _,
                                                       _ ->
                    selectClassStartTime = content
                    etClassStartTime.value = content
                }
            }
            //下课时间
            R.id.etClassEndTime -> {
                ePickerUtil?.showEndTimePicker("") { content,
                                                     _,
                                                     _ ->
                    selectClassEndTime = content
                    etClassEndTime.value = content
                }
            }
            //提交
            R.id.btnSubmit -> {
                className = etClassName.value
                if (className.isEmpty()) {
                    ToastUtil.show(context, "请填写班级名称")
                    return
                }
                createOfficialClass()
            }
        }
    }

    private fun createOfficialClass() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "gzaAddClassGrade",
            "eid" to LocalCacheUtil.getUid(),
            "class_name" to className,
            "class_type_dic_id" to "57",//（57正式班58体验班59临时班）
            "start_time" to selectClassStartTime,
            "end_time" to selectClassEndTime,
            "class_room_id" to selectClassRoom,
            "stage_id" to selectCourseState,
            "course_type_id" to selectCourseType,
            "start_time_id" to selectCourseWare,
            "createtime" to selectClassStartDate,
            "classify_id" to classType,
            "school_id" to LocalCacheUtil.getSchoolId(),
            "courseware_id" to "",
            "course_type" to selectCourseKind,
            "course_quantity" to selectCourseNumber
        )

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<Any>(mContext) {
                override fun onSuccess(data: Any, msg: String) {
                    ToastUtil.show(mContext, "创建成功")
                }
            })
    }
}